package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Message;
import cn.wolfcode.trip.base.domain.MessageContent;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.CrowdMapper;
import cn.wolfcode.trip.base.mapper.MessageContentMapper;
import cn.wolfcode.trip.base.mapper.MessageMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.friend.MessageQuery;
import cn.wolfcode.trip.base.service.IFriendService;
import cn.wolfcode.trip.base.service.IMessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MessageServiceImpl implements IMessageService{
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private MessageContentMapper contentMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private IFriendService friendService;

    @Override
    public void sendMassage(Message message) {
        if (message.getUser() == null || message.getUser().getId() == null) {
            throw new RuntimeException("请先登录再发送");
        }
        message.setState(Message.STATE_UNREAD);
        message.setCreatdate(new Date());
        if (message.getType() == 2){
            crowdSendMessage(message);
            message.setState(Message.STATE_MARK_READ);    //标记已读是 发送给群的
        }
        messageMapper.insert(message);
        if(message.getContent() != null){
            message.getContent().setId(message.getId());
        }
        contentMapper.insert(message.getContent());
    }

    private void crowdSendMessage(Message message) {
        List<User> users = friendService.getFriendByCrowd(message.getTarget().getId());
        List<Message> list = new ArrayList<>();
        User user1 = new User();
        user1.setId(message.getTarget().getId());
        for (User user : users) {
            if(user.getId() == message.getUser().getId()){
                continue;
            }
            Message msg = new Message();
            msg.setTarget(user);
            msg.setUser(user1);
            msg.setState(Message.STATE_UNREAD);     //标记未读是由群发给人的
            msg.setType(Message.TYPE_CROWD);
            msg.setCreatdate(message.getCreatdate());
            list.add(msg);
        }
        messageMapper.insertList(list);
    }

    public PageInfo getMessage(MessageQuery qo){
        if (qo.getType() ==3) {
            List<Message> list = messageMapper.selectForList(qo);         //加好友的请求
            return new PageInfo(list);
        }else if (qo.getType() ==4){
            List<Map<String,Object>> list = messageMapper.selectForCrowdInvite(qo);                       //群邀请的请求信息
            return new PageInfo(list);
        }
        return new PageInfo();

    }



    @Override
    public Map<String, Object> getInviteList(Long id) {
        MessageQuery qo = new MessageQuery();
        qo.setPageSize(10);
        qo.setTargetId(id);
        //设置查询未读
        qo.setState(1);
        //加好友
        qo.setType(3);
        PageInfo addFriend = getMessage(qo);

        //群邀请
        qo.setType(4);
        PageInfo crowdInvite = getMessage(qo);
        //封装map
        Map<String, Object> map = new HashMap<>();
        map.put("addFriend",addFriend);
        map.put("crowdInvite",crowdInvite);
        return map;
    }

    @Override
    public Map<String, Object> getOneMessage(Long id) {
        Message message = messageMapper.selectByPrimaryKey(id);

        User user = null;
        if(message != null && message.getUser() != null){
            user = userMapper.selectByPrimaryKey(message.getUser().getId());
        }

        Map<String, Object> map = new HashMap<>();
        map.put("message",message);
        map.put("user",user);

        return map;
    }

    @Override
    public void markRead(Long messageId) {
        messageMapper.updateStateById(messageId,Message.STATE_MARK_READ);
    }

    @Override
    public void batchMarkread(Long userId, Long targetId,Integer type) {
        if(type == 2){
            messageMapper.updateStateByUserAndTarget(userId,targetId,Message.STATE_REJECT,type);
        }else{
            messageMapper.updateStateByUserAndTarget(userId,targetId,Message.STATE_MARK_READ,type);
        }
    }

    @Override
    public List<Map<String, Object>> getMessageDialogClue(Long targetId) {
        List<Map<String, Object>> list = messageMapper.selectMessageDialogClue(targetId);
        return list;
    }

    @Override
    public List<Message> getMessageDialog(Long userId, Long targetId, Integer type) {
        List<Message> list = messageMapper.selectMessageByUserTargetTypeState(userId,targetId,type,Message.STATE_UNREAD);
        return list;
    }

    @Override
    public void insert(Message message, int state) {
        MessageContent content = new MessageContent();
        messageMapper.insert(message);
        content.setId(message.getId());
        if (state == 0) {
            //保存内容
            content.setContent("由于内容不合规定没有通过审核");
        } else if (state == 3) {
            content.setContent("已经发布~~~");
        } else if (state == 2) {
            content.setContent("被取消发布了");
        }
        contentMapper.insert(content);
    }

    @Override
    public List<MessageContent> getInviteCrowdMessage(Long crowdId, Long userId) {
        List<MessageContent> list = messageMapper.selectInviteCrowdMessage(crowdId,userId);
        return list;
    }

    @Override
    public List<Message> getMessageCrowdDialog(Long userId, Long targetId, Integer type) {
        if(type != 2){
            return null;
        }
        //先获得最早的未读时间
        Date date = messageMapper.selectForTimeByCrowdAndTarget(userId,targetId);
        System.out.println(date);
        //根据时间查询发送到群的

        List<Message> list = messageMapper.selectForCrowdDialog(userId,date);
        return list;
    }

    @Override
    public List<Map<String, Object>> getMessageCrowdDialogClue(Long id) {
        List<Map<String, Object>> list = messageMapper.selectMessageCrowdDialogClue(id);
        return list;
    }

    @Override
    public Message getAddFriendMessage(Long userId, Long targetId) {
        Message message = messageMapper.getAddFriendMessage(userId,targetId);
        return message;
    }

    @Override
    public void updateSystemState(Long authorId, int state) {
        messageMapper.updateSystemState(authorId,  state,5);
    }

}
